package com.accp.ssmoa.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.accp.ssmoa.pojo.Menu;
import com.accp.ssmoa.service.MenuService;


@Controller()
@Scope("prototype")
@RequestMapping("/admin/menu")
public class MenuController {
	
	@Resource
	private  MenuService service;
	

	
	//实现新增、添加下级菜单、修改菜单
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Map<String,Boolean> saveOrUpdate(Menu menu,HttpServletRequest request) {
		
		Map<String,Boolean> result=new HashMap<String, Boolean>();
		

		
		int a;//修改或添加持久化操作后的返回值
		String opreate= (String)request.getParameter("opreate");//判断操作
		
		
		//判断是进行修改还是添加F
		if(opreate.equalsIgnoreCase("修改菜单")) {
			a=service.updateMenu(menu);
		}else{
			a=service.addMenu(menu);
		}
		
		if(a==1) {
			result.put("success", true);
		}else {
			result.put("success", false);
		}
		
		return result; 
		
	}
	

	//获取弹出选择父级菜单树形图
	@RequestMapping("/getSelectOneMenuPage")
    public String getSelectOneMenuPage(){
    	
    	return ("menu/selectOneMenu");
    }	
	
	
    //显示菜单信息树形图
	@RequestMapping("/getMenuTree")
	@ResponseBody
	public  List<Object>  getMenuTree(){
		
		List<Object> menuTree=service.getMenuTree();
		
		
		return menuTree;
	}



	//获取弹出选择父级菜单树形图
	@RequestMapping("/getChangeIconPage")
    public String getChangeIconPage(@RequestParam("id")Integer id,HttpServletRequest request){
		
		
		request.setAttribute("id", id);
    	return ("menu/changeIcon");
    }



	//实现更改图标
	@RequestMapping(value="/changeIcon")
	@ResponseBody
	public Map<String,Object> changeIcon(@RequestParam(value="icon") String icon,
			@RequestParam(value="id") String id){
		
		
		Menu menu=new Menu();
		
		menu.setId(Integer.parseInt(id));
		menu.setIcon(icon);
		
		Map<String,Object> result=new HashMap<String, Object>();

		int a=service.updateMenu(menu);
		
		if(a>0) {
			result.put("success", true);
			result.put("message", "更改成功");
		}else {
			result.put("message", "更改失败");
		}
		
		return result; 
		
	}




	/*需要进行权限控制的操作，增删改以及授权*/


	//初始化菜单界面
	@RequestMapping("/getListPage")
	public String getListPage(HttpServletRequest request) {
		
		
		List<Menu> menus=service.getAllManu();
		
		
		request.setAttribute("menus",menus);

		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","菜单管理");
		
		request.setAttribute("url","/admin/menu/getListPage");
		
		request.setAttribute("nowBread","管理");
		
		
		return "menu/list";
	}


	//实现删除
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delete(@RequestParam("id")Integer id){

		
		Map<String,Object> result=new HashMap<String, Object>();
		
		int a=0;
		
		a=service.deleteMenu(id);
		
		System.out.println(id);
		
		if(a>0) {
			result.put("success", true);
			result.put("message", "删除成功");
		}else {
			result.put("success", false);
			result.put("message", "删除失败");
		}
		
		return result; 
		
	}


	//提供添加、添加下级机构信息页面
	@RequestMapping("/save")
	public String save(HttpServletRequest request) {
		
		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","菜单管理");
		request.setAttribute("url","/admin/menu/getListPage");
	
		//根据传入的参数判断是进行哪个操作
		if (request.getParameter("parentid")!=null) {
			
			request.setAttribute("nowBread","添加下级菜单");
			
			Integer id=Integer.parseInt(request.getParameter("parentid"));
			
			//根据id查询父级菜单信息
			Menu menu=new Menu();
			try {
				menu = service.getMenuById(id);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			
			Menu menu2=new Menu();
			
			menu2.setParentId(menu.getId());
			menu2.setParent_name(menu.getName());
			
			
			request.setAttribute("menu", menu2);
			
		}else {
			
			request.setAttribute("nowBread","添加新菜单");
		}
		
		
		return "menu/edit";
	}
	
	
	
	//提供修改机构信息页面
	@RequestMapping("/update")
	public String update(HttpServletRequest request) {
		
		//存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle","菜单管理");
		request.setAttribute("url","/admin/menu/getListPage");


		request.setAttribute("nowBread","修改菜单");
			
		Integer id=Integer.parseInt(request.getParameter("id"));
						
		//根据id查询菜单信息
		Menu menu=service.getMenuById(id);
			
		request.setAttribute("menu", menu);
		
		return "menu/edit";
	}


}
